﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDJ.Framework.Core
{
    public abstract class TableTypeSupprotObjectBase
    {
        protected Dictionary<Type, TableTypeSupportBase> baseTypeSupports;

        public TableTypeSupprotObjectBase(Dictionary<Type, TableTypeSupportBase> baseTypeSupports)
        {
            this.baseTypeSupports = baseTypeSupports;
        }
        public abstract List<string> TypeCategory
        {
            get;
        }

        public virtual bool IsArray { get; }
        public virtual bool IsEnum { get; }
        public virtual bool IsObject { get; }
        public virtual Type GetTableType(string typeFullName)
        {
            return ReflectionUtils.GetTypeByTypeFullName(typeFullName, false);
        }

        public virtual object GetTypeDefultValue(Type type)
        {
            return ReflectionUtils.CreateDefultInstance(type);
        }
        public string ObjectValue2TableString(object value)
        {
            if (value == null)
            {
                return "";
            }

            return Object2String(value);
        }
        protected virtual string Object2String(object value)
        {
            return JsonUtils.ToJson(value);
        }

        public object TableStringData2Value(Type type, string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                if (type.IsValueType)
                    return GetTypeDefultValue(type);
                else
                    return null;
            }

            return String2Object(type, data);
        }
        protected virtual object String2Object(Type type, string data)
        {
            return JsonUtils.FromJson(type, data);
        }
    }

}
